package cn.edu.ujn.food.controller;

import cn.edu.ujn.food.entity.*;
import cn.edu.ujn.food.service.*;
import cn.edu.ujn.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@org.springframework.stereotype.Controller
public class DishController {
    @Autowired
    private UserService userService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @ModelAttribute("categoryMap")
    public Map<Integer, String> getCategoryMap() {
        List<Category> categories = categoryService.findAll();
        Map<Integer, String> categoryMap = new HashMap<>();
        for (Category cate : categories) {
            categoryMap.put(cate.getCateId(), cate.getCateName());
        }
        return categoryMap;
    }

    @GetMapping("/dish/{id}")
    @ResponseBody
    public ResponseEntity<Dish> getDishById(@PathVariable("id") Integer dishId) {
        Dish dish = dishService.queryById(dishId);
        if (dish == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
        return new ResponseEntity<>(dish, HttpStatus.OK);
    }

    @PutMapping("/dish/{id}")
    @ResponseBody
    public ResponseEntity<Map<String, Object>> updateDish(@PathVariable("id") Integer dishId, @RequestBody Dish dish) {
        try {
            dish.setDishId(dishId);
            dishService.update(dish);
            return ResponseEntity.ok(Map.of("success", true, "message", "菜品信息已成功更新"));
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(Map.of("success", false, "message", "更新失败: " + e.getMessage()));
        }
    }

    @DeleteMapping("/dish/{id}")
    public ResponseEntity<Map<String, Object>> deleteDish(@PathVariable("id") Integer dishId) {
        try {
            boolean isDeleted = dishService.deleteById(dishId);
            if (isDeleted) {
                return ResponseEntity.ok(Map.of("success", true, "message", "菜品信息删除成功！"));
            } else {
                return new ResponseEntity<>(Map.of("success", false, "message", "菜品信息删除失败，菜品不存在或已删除。"), HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(Map.of("success", false, "message", "菜品信息删除失败: " + e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @PostMapping("/dish")
    @ResponseBody
    public String createDish(Dish dish) {
        try {
            dishService.insert(dish);
            return "OK";
        } catch (Exception e) {
            return "FAIL";
        }
    }

    @GetMapping("/dish")
    public String listDish(@RequestParam(value = "page", defaultValue = "1") Integer page,
                           @RequestParam(value = "row", defaultValue = "10") Integer rows, Model model, Dish dish) {
        dish.setStart((page - 1) * rows);
        dish.setRows(rows);
        Page<Dish> page4 = dishService.selectAll(dish);
        page4.setPage(page);
        model.addAttribute("page", page4);
        return "dish";
    }
}


